package com.usian.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbContentMapper;
import com.usian.pojo.TbContent;
import com.usian.redis.RedisClient;
import com.usian.service.ContentService;
import com.usian.utils.AdNode;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chen
 * @date 2021/1/11 - 20:09 - 星期一
 * @project usian_parent
 * @package com.usian.service.Impl
 * @by GGbomd猪猪侠
 */
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper tbContentMapper;

    //读取配置文件里的东西
    @Value("${AD_CATEGORY_ID}")
    private Long AD_CATEGORY_ID;

    @Value("${AD_HEIGHT}")
    private Integer AD_HEIGHT;

    @Value("${AD_WIDTH}")
    private Integer AD_WIDTH;

    @Value("${AD_HEIGHTB}")
    private Integer AD_HEIGHTB;

    @Value("${AD_WIDTHB}")
    private Integer AD_WIDTHB;

    @Value("${PORTAL_AD_KEY}")
    private String PORTAL_AD_KEY;

    @Autowired
    private RedisClient redisClient;


    /**
     * 根据分类查询内容
     * @param page 当前页
     * @param rows 每页显示几条数据
     * @param categoryId 分类的id
     * @return
     */
    @Override
    public PageResult selectTbContentAllByCategoryId(Integer page, Integer rows, Long categoryId) {
        // 分页插件
        PageHelper.startPage(page,rows);
        // 根据分类id查询内容
        QueryWrapper<TbContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId);
        List<TbContent> tbContents = tbContentMapper.selectList(queryWrapper);
        PageInfo<TbContent> pageInfo = new PageInfo<>(tbContents);
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(pageInfo.getPageNum());//当前页
        pageResult.setTotalPage(pageInfo.getPages());//总页数
        pageResult.setResult(pageInfo.getList());//返回一个结果集
        return pageResult;
    }

    /**
     * 根据分类添加内容
     * @param tbContent
     * @return
     */
    @Override
    public Integer insertTbContent(TbContent tbContent) {
        tbContent.setUpdated(new Date());
        tbContent.setCreated(new Date());
        //缓存同步
        redisClient.hdel(PORTAL_AD_KEY,AD_CATEGORY_ID.toString());
        return tbContentMapper.insertSelective(tbContent);
    }

    /**
     *  删除分类下的内容
     * @param ids
     * @return
     */
    @Override
    public Integer deleteContentByIds(Long ids) {
        //缓存同步
        redisClient.hdel(PORTAL_AD_KEY,AD_CATEGORY_ID.toString());
        return tbContentMapper.deleteByPrimaryKey(ids);
    }

    /**
     * 查询首页大广告位
     * 操作 tb_content
     * @return
     */
    @Override
    public List<AdNode> selectFrontendContentByAD() {
        //查询缓存
        List<AdNode> adNodeListRedis =
                (List<AdNode>)redisClient.hget(PORTAL_AD_KEY,AD_CATEGORY_ID.toString());
        if(adNodeListRedis!=null){
            // 不等于空直接返回
            return adNodeListRedis;
        }
        // 查询TbContent
        QueryWrapper<TbContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",AD_CATEGORY_ID);
        List<TbContent> tbContents = tbContentMapper.selectList(queryWrapper);
        // 往集合里存放
        List<AdNode> adNodeList = new ArrayList<AdNode>();
        for (TbContent tbContent : tbContents) {
            // 没循环一次初始化一次
            AdNode adNode = new AdNode();
            adNode.setSrc(tbContent.getPic());
            adNode.setSrcB(tbContent.getPic2());
            adNode.setHref(tbContent.getUrl());
            adNode.setHeight(AD_HEIGHT);
            adNode.setWidth(AD_WIDTH);
            adNode.setHeightB(AD_HEIGHTB);
            adNode.setWidthB(AD_WIDTHB);
            adNodeList.add(adNode);
        }
        //添加到缓存
        redisClient.hset(PORTAL_AD_KEY,AD_CATEGORY_ID.toString(),adNodeList);
        return adNodeList;
    }
}
